import {
	createRouter,
	createWebHashHistory
} from "vue-router";
import {
	showConfirmDialog
} from 'vant';
import {
	getTokenLocal
} from "@/utils/auth.js";

const routes = [{
		path: "/",
		redirect: "/home",
		meta: {
			title: "首页"
		}
	},
	{
		path: '/home',
		name: 'home',
		component: () => import("@/views/home/Home.vue"),
		meta: {
			title: "首页",
		}
	},
	{
		path: "/dinnerList",
		component: () => import("@/views/dinner/DinnerList.vue"),
		name: "dinnerList",
		meta: {
			title: "正餐优选列表"
		}
	},
	{
		path: "/dinner/:id",
		component: () => import("@/views/dinner/DinnerDetail.vue"),
		name: "dinnerDetail",
		meta: {
			title: "正餐优选详情"
		}
	},
	{
		path: "/order/list",
		component: () => import("@/views/order/orderList.vue"),
		name: "orderList",
		meta: {
			title: "我的订单",
			requiresAuth: true
		},
	},
	{
		path: '/feedback',
		component: () => import('@/views/Feedback.vue'),
		name: 'feedback',
		meta: {
			title: "留言反馈"
		}
	},
	{
		path: '/contact-us',
		component: () => import('@/views/ContactUs.vue'),
		name: 'contact-us',
		meta: {
			title: "联系我们"
		}
	},
	{
		path: '/verticalTabs',
		component: () => import("@/views/product/VerticalTabs.vue"),
		name: "verticalTabs",
		meta: {
			title: "商品"
		}
	},
	{
		path: "/productDetail/:id",
		name: "productDetail",
		component: () => import('@/views/product/ProductDetail.vue'),
		meta: {
			title: "商品详情"
		}
	},
	{
		path: "/shopcart",
		component: () => import("@/views/shopcart/Shopcart.vue"),
		name: "shopcart",
		meta: {
			title: "购物车",
			requiresAuth: true
		}
	},
	{
		path: '/user',
		name: 'user',
		component: () => import('@/views/user/User.vue'),
		meta: {
			title: "用户",
			requiresAuth: true
		}
	},
	{
		path: '/login',
		name: 'login',
		component: () => import('@/views/user/Login.vue'),
		meta: {
			title: "登录",
			requiresAuth: false
		}
	},
	{
		path: '/register',
		name: 'register',
		component: () => import('@/views/user/Register.vue'),
		meta: {
			title: "注册",
			requiresAuth: false
		}
	},
	{
		path: "/user/profilePage",
		component: () => import("@/views/user/ProfilePage.vue"),
		name: "profilePage",
		meta: {
			title: "个人主页"
		}
	},
	{
		path: "/user/profile",
		component: () => import("@/views/user/Profile.vue"),
		name: "profile",
		meta: {
			title: "个人资料"
		}
	},
	{
		path: "/review/review",
		component: () => import("@/views/review/Review.vue"),
		name: "review",
		meta: {
			title: "待评价"
		}
	},
	{
		path: "/user/updatePwd",
		component: () => import("@/views/user/UpdatePwd.vue"),
		name: "updatePwd",
		meta: {
			title: "修改密码"
		}
	},
	{
		path: "/order/create",
		component: () => import("@/views/order/OrderCreate.vue"),
		name: "orderCreate",
		meta: {
			title: "新建订单",
			requiresAuth: true
		},
	},
	{
		path: "/user/addressList",
		component: () => import("@/views/address/AddressList.vue"),
		name: "addressList",
		meta: {
			title: "收货地址列表",
			requiresAuth: true
		}
	},
	{
		path: "/user/addressEdit/:id",
		component: () => import("@/views/address/AddressEdit.vue"),
		props: true,
		name: "addressEdit",
		meta: {
			title: "收货地址",
			requiresAuth: true
		}
	},
	{
		path: "/order/list",
		component: () => import("@/views/order/OrderList.vue"),
		name: "orderList",
		meta: {
			title: "我的订单",
			requiresAuth: true
		}
	},
	{
		path: '/payment/:orderId',
		name: 'PaymentPage',
		component: () => import('@/views/order/PaymentPage.vue'), // 修复：添加逗号
		meta: {
			title: "支付页面",
			requiresAuth: true
		}
	},
	{
		path: "/order/detail/:id",
		component: () => import("@/views/order/OrderDetail.vue"),
		props: true,
		name: "orderDetail",
		meta: {
			title: "订单详情",
			requiresAuth: true
		}
	},
	{
		path: '/myFavorites',
		name: 'myFavorites',
		props: true,
		component: () => import("@/views/MyFavorites.vue"),
		meta: {
			title: "收藏"
		}
	},
	{
		path: '/history',
		name: 'history',
		props: true,
		component: () => import("@/views/History.vue"),
		meta: {
			title: "浏览历史"
		}
	},
	{
		path: '/helpCenter',
		name: 'helpCenter',
		props: true,
		component: () => import("@/views/helpCenter.vue"),
		meta: {
			title: "帮助中心"
		}
	},
	{
		path: '/setting',
		name: 'Setting',
		props: true,
		component: () => import("@/views/Setting.vue"),
		meta: {
			title: "设置"
		}
	},
	{
		path: "/user/accountSec",
		component: () => import("@/views/user/AccountSec.vue"),
		name: "accountSec",
		meta: {
			title: "账号安全"
		}
	},
	{
		path: "/updatePwd",
		component: () => import("@/views/user/UpdatePwd.vue"),
		name: "updatePwd",
		meta: {
			title: "修改密码"
		}
	},
	{
		path: '/forgotPassword',
		name: 'forgotPassword',
		component: () => import('@/views/user/RePassword.vue'),
		meta: {
			requiresAuth: false
		}
	},
	{
		path: "/captchaLogin",
		component: () => import("@/views/user/CaptchaLogin.vue"),
		name: "captchaLogin",
		meta: {
			title: "验证码登录"
		}
	},
	{
		path: "/thirdLogin",
		component: () => import("@/views/user/ThirdLogin.vue"),
		name: "thirdLogin",
		meta: {
			title: "第三方登录"
		}
	},
	{
		path: "/:pathMatch(.*)",
		name: "notFound",
		component: () => import('@/views/NotFound.vue'),
		meta: {
			title: "导航页面不存在"
		}
	}
];

const router = createRouter({
	history: createWebHashHistory(),
	routes
});

router.beforeEach((to, from, next) => {
	const isAuthenticated = !!getTokenLocal();
	if (to.meta.requiresAuth) {
		if (isAuthenticated) {
			next();
		} else {
			showConfirmDialog({
				message: "请先登录"
			}).then(() => {
				next({
					name: 'login',
					query: {
						redirect: to.path
					}
				});
			}).catch(() => {
				next({
					name: 'home'
				});
			});
		}
	} else {
		next();
	}
});

export default router;